﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;


namespace NovelL
{
    public class CommandHandler : MonoBehaviour
    {
        private int _index = 0;
        private TextAsset _textAsset;
        private string _context;
        private string[] commands;
        private ResLoader resLoader = ResLoader.Allocate();

        private bool exec = true; //判断当前是否需要连续执行脚本命令



        // Start is called before the first frame update
        void Start()
        {
            _textAsset = resLoader.LoadSync<TextAsset>("gamescript");
            HandleCommand();
        }

        // Update is called once per frame
        void Update()
        {
            if (exec)
            {
                ExecuteCommand(_index);
            }
        }

        void HandleCommand()
        {
            _context = _textAsset.text;
            commands = _context.Split('\n');
            int i = 0;
            while (i < commands.Length)
            {
                Debug.Log(commands[i]);
                i++;
            }


        }

        void ExecuteCommand(int index)
        {
            _index = index;
            if (_index > commands.Length - 1)
            {
                exec = false;
                return;
            }
            string[] singleCommand = commands[_index].Split(' ');
            if (singleCommand[0] == null)
            {
                throw new System.Exception("无内容的指令!");
            }
            switch (singleCommand[0])
            {
                case "SetBGM":
                    if (singleCommand[1] == null)
                    {
                        throw new System.Exception("未指定背景音乐的路径");
                    }
                    string path = singleCommand[1];
                    AudioKit.PlayMusic(path);
                    break;

                case "SetBPNG":
                    Debug.Log("set");
                    if (singleCommand[1] == null)
                    {
                        throw new System.Exception("未指定背景图片的路径");
                    }
                    string imgpath = singleCommand[1];

                    break;
                case "StopBGM":
                    AudioKit.StopMusic();
                    break;
                case "Dialog":
                    exec = false;
                    break;

                default:
                    exec = false;
                    throw new System.Exception("脚本第" + (_index + 1).ToString() + "行是未知的指令,脚本运行已经停止");
                    break;
            }
            _index++;
        }

        void ExecuteCommand()
        {
            ExecuteCommand(_index);
        }


    }
}






